﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common.Dao;
using Common.Model;
using Common.Redis;
using Spider.Mode;
using Spider.Spider;

namespace Spider.Script
{
    public class YunFincalScript
    {
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(YunFincalScript));
        
        //保存和更新云财经新闻
        public static void Run()
        {
            String from = "云财经";
            var startTime = DateTime.Today.AddDays(-1);

            //抓取新闻
            YunFincalSpider yunFincalSpider = new YunFincalSpider();
            var lastNews = NewsDao.GetLastNews(@from);
            List<News> news = yunFincalSpider.GetNews(lastNews.SiteTime, DateTime.Now);
            news.ForEach(m =>
            {
                Log.Info("title:"+m.Title);
                Log.Info("abs:"+m.Abs);
                Log.Info("type:"+m.Type);
                Log.Info("==================");
            });
            //保存新闻
            if (news != null && news.Count > 0)
            {
                DateTime now = DateTime.Now;

                startTime = news.First().SiteTime;
                foreach (var item in news)
                {
                    item.CrawlingTime = now;
                    if (item.SiteTime > startTime)
                    {
                        startTime = item.SiteTime;
                    }
                }

                NewsDao.BatchSaveNews(news);

                //更新相关tag的最新日期到redis
                NewsRedisDbProvider.SetLastNewsDatetime(from, startTime);
            }
        }
    }
}